package cn.orange.ch07_binarytree;

import cn.orange.TreeNode;

/**
 * LC404.左叶子之和
 */
public class LC404 {
    public int sumOfLeftLeaves(TreeNode root) {
        if (root == null) return 0;
        if (root.left == null && root.right == null) return 0;
        int leftVal = sumOfLeftLeaves(root.left);
        int rightVal = sumOfLeftLeaves(root.right);
        if (root.left != null && root.left.left == null && root.left.right == null) {
            leftVal = root.left.val;
        }
        return leftVal + rightVal;
    }
    public static void main(String[] args) {
        LC404 alg = new LC404();
        TreeNode root = new TreeNode(3, new TreeNode(9), new TreeNode(20));
        root.right.left = new TreeNode(15);
        root.right.right = new TreeNode(7);
        System.out.println(alg.sumOfLeftLeaves(root));
    }
}
